﻿Imports System
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.Web.Configuration

Namespace ASPNET.StarterKit.Setup


    ''' <summary> 
    ''' Summary description for Utility 
    ''' </summary> 
    Public Class SetupUtility
        ' This string needs to added to the web.config/connectionStrings section on new applications. 
        ' This is what tells the application that it's being used for the first time. 
        ' You can put anything here, as long as it matches what is in the connectionString attribute value 
        ' in the connectionStrings section of web.confg. 
        Private Const NOTIMPLEMENTED As String = "##NOT_IMPLEMENTED##"

        ''' <summary> 
        ''' Check all connection strings for ##NOT_IMPLEMENTED##. If it exists 
        ''' in the connections strings section, it is the first time and we need 
        ''' to redirect to the install folder. 
        ''' </summary> 
        ''' <returns></returns> 
        Public Shared ReadOnly Property isFirstTime() As Boolean
            Get
                Dim firstTime As Boolean = False
                Dim connStrings As ConnectionStringSettingsCollection = WebConfigurationManager.ConnectionStrings
                For Each conn As ConnectionStringSettings In connStrings
                    If conn.ConnectionString = NOTIMPLEMENTED Then
                        firstTime = True
                        Exit For
                    End If
                Next
                Return firstTime
            End Get
        End Property



        ''' <summary> 
        ''' You can return a control of a Type based on the ID and the starting Control. 
        ''' </summary> 
        ''' <typeparam name="T"></typeparam> 
        ''' <param name="startingControl"></param> 
        ''' <param name="id"></param> 
        ''' <returns></returns> 
        Public Shared Function FindControl(Of T As System.Web.UI.Control)(ByVal startingControl As System.Web.UI.Control, ByVal id As String) As T
            Dim found As T = Nothing
            For Each activeControl As System.Web.UI.Control In startingControl.Controls
                found = TryCast(activeControl, T)
                If found Is Nothing Then
                    found = FindControl(Of T)(activeControl, id)
                ElseIf String.Compare(id, found.ID, True) <> 0 Then
                    found = Nothing
                End If
                If found IsNot Nothing Then
                    Exit For
                End If
            Next
            Return found
        End Function
    End Class

End Namespace
